package com.atguigu.serviceedu.controller.front;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.frontVo.CourseWebVo;
import com.atguigu.serviceedu.entity.frontVo.FrontCourseVo;
import com.atguigu.serviceedu.entity.vo.ChapterVo;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/serviceedu/coursefront")
//@CrossOrigin
public class CourseFrontController {

    //课程查询
    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduCourseService eduCourseService;

    @PostMapping("pageCourse/{page}/{limit}")
    public R pageCourse(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) FrontCourseVo frontCourseVo) {

        Page<EduCourse> pageCourse = new Page<>(page, limit);

        Map<String, Object> map = eduCourseService.getPageCourse(pageCourse,frontCourseVo);

        return R.ok().data(map);
    }

    //查询课程详情
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable("courseId")String courseId){
        //根据课程id查询课程信息
        CourseWebVo courseWebVo = eduCourseService.selectCourseInfo(courseId);

        //查询章节小节信息
        List<ChapterVo> allChapter = eduChapterService.getAllChapter(courseId);

        return R.ok().data("courseWebVo",courseWebVo).data("allChapter",allChapter);
    }
}
